<template>
  <el-form inline="true">
    <el-form-item label="创建日期">
      <el-date-picker v-model="datetimeRange" style="width: 240px" type="datetimerange" range-separator="至"
                      start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
    </el-form-item>
    <el-form-item label="竞赛名称">
      <el-input v-model="params.title" placeholder="请您输入要搜索的竞赛名称" />
    </el-form-item>
    <el-form-item>
      <el-button @click="onSearch" plain>搜索</el-button>
      <el-button @click="onReset" plain type="info">重置</el-button>
      <el-button type="primary" :icon="Plus" plain @click="onAddExam">添加竞赛</el-button>
    </el-form-item>
  </el-form>
  <!-- 表格 -->
  <el-table height="526px" :data="examList">
    <el-table-column prop="title" label="竞赛标题"/>
    <el-table-column prop="startTime" width="180px" label="竞赛开始时间" />
    <el-table-column prop="endTime" width="180px" label="竞赛结束时间" />
    <el-table-column label="是否开赛" width="100px">
      <template #default="{ row }">
        <div v-if="!isNotStartExam(row)">
          <el-tag type="warning">已开赛</el-tag>
        </div>
        <div v-else>
          <el-tag type="info">未开赛</el-tag>
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="status" width="100px" label="是否发布">
      <template #default="{ row }">
        <div v-if="row.status === 0">
          <el-tag type="danger">未发布</el-tag>
        </div>
        <div v-if="row.status === 1">
          <el-tag type="success">已发布</el-tag>
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="createName" width="140px" label="创建用户" />
    <el-table-column prop="createTime" width="180px" label="创建时间" />
    <el-table-column label="操作" width="180px">
      <template #default="{ row }">
        <el-button v-if="isNotStartExam(row) && row.status === 0" type="text" @click="onEdit(row.examId)">编辑
        </el-button>
        <el-button v-if="isNotStartExam(row) && row.status === 0" type="text" @click="onDelete(row.examId)" class="red">删除
        </el-button>
        <el-button v-if="row.status === 1 && isNotStartExam(row)" type="text"
                   @click="cancelPublishExam(row.examId)">撤销发布</el-button>
        <el-button v-if="row.status === 0 && isNotStartExam(row)" type="text"
                   @click="publishExam(row.examId)">发布</el-button>
        <el-button type="text" v-if="!isNotStartExam(row)">已开赛，不允许操作</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页区域 -->
  <el-pagination background size="small" layout="total, sizes, prev, pager, next, jumper" :total="total"
                 v-model:current-page="params.pageNum" v-model:page-size="params.pageSize" :page-sizes="[5, 10, 15, 20]"
                 @size-change="handleSizeChange" @current-change="handleCurrentChange" />
</template>

<script setup>
import { Plus } from '@element-plus/icons-vue'
import { reactive,ref } from 'vue'
import router from '@/router'

// data
const datetimeRange = ref([])
const examList = reactive([])

const params = reactive({
  pageNum: 1,
  pageSize: 10,
  startTime:'',
  endTime:'',
  title: ''
})

// method



function isNotStartExam(exam) {
  const now = new Date(); //当前时间
  return new Date(exam.startTime) > now
}


function handleSizeChange(newSize) {
  console.log('每页显示条数', newSize)

}

function handleCurrentChange(newPage) {
  console.log('当前页', newPage)
}


function getExamList() {
  console.log('获取竞赛列表')
  // 静态数据
  mockStaticData()

}

function mockStaticData() {
  examList.push({
    examId: 1,
    title: '2021年全国大学生程序设计竞赛',
    startTime: '2021-10-01 08:00:00',
    endTime: '2021-10-01 12:00:00',
    status: 1,
    createName: '张三',
    createTime: '2021-09-01 08:00:00'
  })
  examList.push({
    examId: 2,
    title: '2021年全国大学生数学建模竞赛',
    startTime: '2021-10-01 08:00:00',
    endTime: '2021-10-01 12:00:00',
    status: 0,
    createName: '李四',
    createTime: '2021-09-01 08:00:00'
  })
  examList.push({
    examId: 3,
    title: '2021年全国大学生英语竞赛',
    startTime: '2021-10-01 08:00:00',
    endTime: '2021-10-01 12:00:00',
    status: 0,
    createName: '王五',
    createTime: '2021-09-01 08:00:00'
  })
  examList.push({
    examId: 4,
    title: '2021年全国大学生物理竞赛',
    startTime: '2025-10-01 08:00:00',
    endTime: '2026-10-01 12:00:00',
    status: 0,
    createName: '赵六',
    createTime: '2021-09-01 08:00:00'
  })
  examList.push({
    examId: 5,
    title: '2021年全国大学生物理竞赛',
    startTime: '2025-10-01 08:00:00',
    endTime: '2026-10-01 12:00:00',
    status: 1,
    createName: '赵六',
    createTime: '2021-09-01 08:00:00'
  })
}


getExamList()

function onSearch() {
  console.log('搜索')

}

function onReset() {
  console.log('重置')
}

function onAddExam() {
  console.log('添加竞赛')
}

async function onEdit(examId) {
  console.log('编辑竞赛, examId:', examId)
}

async function onDelete(examId) {
  console.log('删除竞赛, examId:', examId)
}

async function publishExam(examId) {
  console.log('发布竞赛, examId:', examId)
}

async function cancelPublishExam(examId) {
  console.log('撤销发布竞赛, examId:', examId)
}

</script>